package com.lw.common.ext

import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.lw.common.base.AppContext

//-------------------------drawableTop--------------------------

fun TextView.drawableTop(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, drawable, null, null)
}

fun TextView.drawableTop(@DrawableRes id: Int, size: Int) {
    drawableTop(id, size, size)
}

fun TextView.drawableTop(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableTop(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-----------------------drawableBottom-------------------------

fun TextView.drawableBottom(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, null, null, drawable)
}

fun TextView.drawableBottom(@DrawableRes id: Int, size: Int) {
    drawableBottom(id, size, size)
}

fun TextView.drawableBottom(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableBottom(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-------------------------drawableLeft--------------------------

fun TextView.drawableLeft(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(drawable, null, null, null)
}

fun TextView.drawableLeft(@DrawableRes id: Int, size: Int) {
    drawableLeft(id, size, size)
}

fun TextView.drawableLeft(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableLeft(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-------------------------drawableRight--------------------------

fun TextView.drawableRight(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, null, drawable, null)
}

fun TextView.drawableRight(@DrawableRes id: Int, size: Int) {
    drawableRight(id, size, size)
}

fun TextView.drawableRight(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableRight(id, drawable.minimumWidth, drawable.minimumHeight)
}

fun TextView.textMarquee() {
    this.ellipsize = TextUtils.TruncateAt.MARQUEE
    this.isSingleLine = true
    this.isSelected = true
    this.isFocusable = false
    this.isFocusableInTouchMode = false
    this.setHorizontallyScrolling(true)
}